package com.getpebble.android.framework.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.framework.comm.MessageRouter;
import com.getpebble.android.framework.endpoint.EndpointRequest;
import com.getpebble.android.framework.endpoint.EndpointRequest.EndpointAction;
import com.getpebble.android.framework.protocol.EndpointId;
import java.util.Set;

public class TimeReceiver extends BroadcastReceiver {
    private static final String TAG = TimeReceiver.class.getSimpleName();

    public void onReceive(Context context, Intent intent) {
        if (intent == null) {
            Trace.warning(TAG, "Received null intent; dropping");
            return;
        }
        String action = intent.getAction();
        if (action == null) {
            Trace.warning(TAG, "Intent has null action");
            return;
        }
        boolean isTimeChanged = action.equals("android.intent.action.TIME_SET") || action.equals("android.intent.action.TIMEZONE_CHANGED");
        if (isTimeChanged) {
            Trace.debug(TAG, "Time changed");
            for (MessageRouter router : getAllRouters()) {
                router.onRequest(new EndpointRequest(EndpointId.TIME, EndpointAction.SEND_SET_TIME_MESSAGE), null);
            }
            return;
        }
        Trace.warning(TAG, "Action is not time changed: " + action);
    }

    protected Set<MessageRouter> getAllRouters() {
        return MessageRouter.getAllRouters();
    }
}
